$(function () {
    $("#jqGrid").Grid({
        url: '../otcchangelog/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			// {label: '转换失败的用户id', name: 'userid', index: 'userid', width: 80},
			{label: '用户昵称', name: 'nickname', index: 'nickname', width: 80,align:'center'},
			{label: '手机号码', name: 'phone', index: 'phone', width: 80,align:'center'},
			{label: '类型', name: 'type', index: 'type', width: 80,align:'center',
				formatter:function (value) {
					if(value==1){
						return "SPC转化DSPC";
					}else if(value==2){
						return  "提链申请(冻结DSPC)";
					}else if(value==3){
						return	"提链成功(解冻DSPC)";
					}else if(value==4){
						return	"提链失败(解冻DSPC并还原)";
					}else if(value==5){
						return "买入";
					}else if(value==6){
						return	"卖出";
					}else if(value==7){
						return	"手续费";
					}else if(value==8){
						return	"团队奖励";
					}else if(value==10){
						return	"平台收益";
					}else if(value==11){
						return	"客服操作";
					}
				}
			},
			{label: 'orderid', name: 'orderid', index: 'orderid', width: 80,align:'center',hidden:true},
			{label: '订单编号', name: 'orderno', index: 'orderno', width: 80,align:'center'},
			{label: '结果', name: 'status', index: 'status', width: 80,align:'center',
				formatter:function (value) {
					if(value==0){
						return "失败";
					}else if(value==1){
						return "成功";
					}
				}
			},
			{label: '消息', name: 'msg', index: 'msg', width: 80,align:'center'},
			{label: '数量', name: 'count', index: 'count', width: 80,align:'center'},
			{label: '添加时间', name: 'addTime', index: 'add_time', width: 80,align:'center',
				formatter:function (value) {
					return transDate(value, 'yyyy-MM-dd hh:mm:ss');
				}
			},
			{
				name:'查看', label:'查看', width: 30,formatter:function(cellvalue, options, row){
					return '<a href="#" onclick="vm.look(\''+row.id+'\',\''+row.orderid+'\')">查看</a>';
				}
			}]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
		showCard:false,
        title: null,
		otcChangeLog: {},
		otcTradeorder: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: '',
			status:'',
			type:''
		},
		users:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.otcChangeLog = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
			vm.queryUserList();
		},
		saveOrUpdate: function (event) {
            let url = vm.otcChangeLog.id == null ? "../otcchangelog/save" : "../otcchangelog/update";
			vm.otcChangeLog.userid =$('#userselect option:selected').text();
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.otcChangeLog),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		look:function(id,orderid){
			vm.showList = false;
			vm.title = "查看";
			vm.otcChangeLog = {};
			vm.users = [];
			vm.getInfo(id);
			vm.queryUserList();

			vm.otcTradeorder = {};
			if(orderid!="null"){
				console.log(1,orderid)
				vm.getOrderInfo(orderid);
				console.log(2,orderid)
				vm.showCard = false;
			}else{
				vm.showCard = true;
			}
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../otcchangelog/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../otcchangelog/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.otcChangeLog = r.otcChangeLog;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name,'status':vm.q.status,'type':vm.q.type},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
				name: '',
				status:'',
				type:''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
		queryUserList:function () {
			Ajax.request({
				url: "../sportsuser/queryAll",
				async: true,
				successCallback: function (r) {
					vm.users = r.list;
				}
			});
		},getOrderInfo:function (id) {
			Ajax.request({
				url: "../otctradeorder/info/" + id,
				async: true,
				successCallback: function (r) {
					vm.otcTradeorder = r.otcTradeorder;
				}
			});
		}
	}
});